package com.smedi.ismedi.authoritymanagement.core.port.adapter.restful;

import com.smedi.ismedi.authoritymanagement.api.dto.request.ChangeMobileRequest;
import com.smedi.ismedi.authoritymanagement.api.dto.request.ChangePasswordRequest;
import com.smedi.ismedi.authoritymanagement.api.dto.request.CreateAccountBasicRequest;
import com.smedi.ismedi.authoritymanagement.api.dto.request.ForgetPasswordRequest;
import com.smedi.ismedi.authoritymanagement.api.feign.RemoteAccountManagementFeignApi;
import com.smedi.ismedi.authoritymanagement.core.application.command.AccountBasicCommand;
import com.smedi.ismedi.authoritymanagement.core.application.command.ChangeMobileCommand;
import com.smedi.ismedi.authoritymanagement.core.application.command.ChangePasswordCommand;
import com.smedi.ismedi.authoritymanagement.core.application.query.vo.MobileVerificationCodeVo;
import com.smedi.ismedi.authoritymanagement.core.application.service.AccountBasicApplicationService;
import com.smedi.ismedi.authoritymanagement.core.domain.aggregatemodel.AccountBasic;
import com.smedi.ismedi.authoritymanagement.core.port.adapter.service.CallSmsRemoteApiService;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-7-2 10:51
 */
@Slf4j
@RestController
@AllArgsConstructor
public class AccountManagementController implements RemoteAccountManagementFeignApi {
    private final DozerUtil dozerUtil;

    private final AccountBasicApplicationService accountBasicApplicationService;

    private final CallSmsRemoteApiService callSmsRemoteApiService;

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<String> postCreateAccount(CreateAccountBasicRequest createAccountBasicRequest) {
        AccountBasicCommand userBasicCommand = dozerUtil.convertor(createAccountBasicRequest, AccountBasicCommand.class);
        AccountBasic accountBasic = accountBasicApplicationService.generateAccount(userBasicCommand);
        return new ResponseData<Boolean>().success(accountBasic.getUserName());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> postChangePassword(ChangePasswordRequest changePasswordRequest) {
        ChangePasswordCommand changePasswordCommand = dozerUtil.convertor(changePasswordRequest, ChangePasswordCommand.class);
        accountBasicApplicationService.changePassword(changePasswordCommand);
        return new ResponseData<Boolean>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> postChangeMobile(ChangeMobileRequest changeMobileRequest) {
        ChangeMobileCommand changeMobileCommand = dozerUtil.convertor(changeMobileRequest, ChangeMobileCommand.class);
        accountBasicApplicationService.changeMobile(changeMobileCommand);
        return new ResponseData<Boolean>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> postSendVerificationCode(ForgetPasswordRequest forgetPasswordRequest) {
        MobileVerificationCodeVo vo = accountBasicApplicationService.generateVerificationCode(forgetPasswordRequest.getCallAccount());
        callSmsRemoteApiService.sendSimpleMessage(vo.getMobile(), new ArrayList<String>() {{
            add(vo.getVerificationCode());
            add(vo.getMinutes().toString());
        }});

        return new ResponseData<Boolean>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> postSubmitVerificationCode(ForgetPasswordRequest forgetPasswordRequest) {
        accountBasicApplicationService.submitVerificationCode(forgetPasswordRequest.getCallAccount(), forgetPasswordRequest.getVerificationCode());
        return new ResponseData<Boolean>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> postResetPassword(ForgetPasswordRequest forgetPasswordRequest) {
        accountBasicApplicationService.resetPassword(forgetPasswordRequest.getCallAccount(), forgetPasswordRequest.getNewPassword());
        return new ResponseData<Boolean>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> putEnable(Long personId) {
        accountBasicApplicationService.enable(personId);
        return new ResponseData<Boolean>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> putDisable(Long personId) {
        accountBasicApplicationService.disable(personId);
        return new ResponseData<Boolean>().success(true);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public ResponseData<Boolean> deleteAccount(Long personId) {
        accountBasicApplicationService.deleteAccount(personId);
        return new ResponseData<Boolean>().success(true);
    }

    @Override
    public ResponseData<Boolean> deleteAccountOnly(Long personId) {
        accountBasicApplicationService.deleteAccountOnly(personId);
        return new ResponseData<Boolean>().success(true);
    }
}
